package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;
import com.soup.view.value.LoginValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ModifyPass extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private Context bod = new Context(400, 300);
    private JTextField pass = new JTextField();
    private JTextField pass_again = new JTextField();
    private JButton ok = new JButton("修改密码");
    private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    private PersonDao dao = new PersonDao();

    public ModifyPass() {
        this.add(bod);
        this.setTitle("修改密码");
        pass.setBounds(40, 20, bod.getWidth()-80, 40);
        pass_again.setBounds(40, 80, bod.getWidth()-80, 40);
        ok.setBounds(40, 140, bod.getWidth()-80, 40);
        ok.addActionListener(this);
        bod.add(pass, "修改密码", 120);
        bod.add(pass_again, "确认密码", 120);
        bod.add(ok, 20);
        this.setSize(bod.getWidth(), bod.getHeight());
        this.setResizable(false);
        this.setLocation((screen.width-this.getWidth())/2, (screen.height-this.getHeight())/2);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ok) {

            if (pass.getText().equals(pass_again.getText())) {
                Map<String,Object> map = new HashMap<>();
                map.put("id", LoginValue.id);
                List<Person> query = dao.query(map);
                Person person = query.get(0);
                person.setPassword(pass.getText());

                int update = dao.update(person);
                if (update > 0){
                    MassageTip.showMassage(null,"修改成功");
                }else {
                    MassageTip.showMassage(null,"修改失败");
                }
            }else{
                MassageTip.showMassage(null,"两次密码不相同");
            }
        }

    }
}